﻿using System;
using System.Linq;

namespace Kernelx.Extensions
{
    /// <summary>
    /// 时间类型的扩展方法
    /// </summary>
    public static class DateTimeExtensions
    {

        /// <summary>
        /// 转换为时间类型的字符串
        /// </summary>
        /// <param name="s">时间</param>
        /// <param name="format">格式</param>
        /// <returns></returns>
        public static string ToDateTimeString(this DateTimeOffset s, string format = "yyyy-MM-dd HH:mm:ss")
        {
            return s.ToString(format);
        }

        /// <summary>
        /// 转换为时间类型的字符串
        /// </summary>
        /// <param name="s">时间</param>
        /// <param name="format">格式</param>
        /// <returns></returns>
        public static string ToDateTimeString(this DateTime? s, string format = "yyyy-MM-dd HH:mm:ss")
        {
            return s != null ? Convert.ToDateTime(s).ToString(format) : null;
        }
        /// <summary>
        /// 转换为日期类型的字符串
        /// </summary>
        /// <param name="s">时间</param>
        /// <param name="format">格式</param>
        /// <returns></returns>
        public static string ToDateString(this DateTime s, string format = "yyyy-MM-dd")
        {
            return s.ToString(format);
        }
        /// <summary>
        /// 转换为日期类型的字符串
        /// </summary>
        /// <param name="s">时间</param>
        /// <param name="format">格式</param>
        /// <returns></returns>
        public static string ToDateString(this DateTime? s, string format = "yyyy-MM-dd")
        {
            return s != null ? Convert.ToDateTime(s).ToString(format) : null;
        }


        private static readonly DayOfWeek[] _weekends = { DayOfWeek.Saturday, DayOfWeek.Sunday };
        /// <summary>
        /// 当前时间是否周末
        /// </summary>
        /// <param name="dateTime">时间点</param>
        /// <returns></returns>
        public static bool IsWeekend(this DateTime dateTime)
        {
            return _weekends.Contains(dateTime.DayOfWeek);
        }

        private static readonly DayOfWeek[] _weekdays = { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };
        /// <summary>
        /// 当前时间是否工作日
        /// </summary>
        /// <param name="dateTime">时间点</param>
        /// <returns></returns>
        public static bool IsWeekday(this DateTime dateTime)
        {
            return _weekdays.Contains(dateTime.DayOfWeek);
        }

        /// <summary>
        /// 获取时间相对唯一字符串
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="milsec">是否使用毫秒</param>
        /// <returns></returns>
        public static string ToUniqueString(this DateTime dateTime, bool milsec = false)
        {
            int seconds = dateTime.Hour * 3600 + dateTime.Minute * 60 + dateTime.Second;
            string value = $"{dateTime:yy}{dateTime.DayOfYear}{seconds}";
            return milsec ? value + dateTime.ToString("fff") : value;
        }

        /// <summary>
        /// 将当前时区时间转换为UTC时间
        /// </summary>
        public static DateTime ToUtcTime(this DateTime dateTime)
        {
            return TimeZoneInfo.ConvertTimeToUtc(dateTime, TimeZoneInfo.Local);
        }

        /// <summary>
        /// 将指定UTC时间转换为当前时区的时间
        /// </summary>
        public static DateTime FromUtcTime(this DateTime dateTime)
        {
            return TimeZoneInfo.ConvertTimeFromUtc(dateTime, TimeZoneInfo.Local);
        }

        /// <summary>
        /// 将时间转换为JS时间格式(Date.getTime())
        /// </summary>
        public static string ToJsGetTime(this DateTime dateTime, bool milsec = true)
        {
            DateTime utc = dateTime.ToUniversalTime();
            TimeSpan span = utc.Subtract(new DateTime(1970, 1, 1));
            return Math.Round(milsec ? span.TotalMilliseconds : span.TotalSeconds).ToString();
        }


    }
}
